import axios from 'axios'
import { ADD_NUMBER, CHANGE_BANNERS, CHANGE_RECOMMENDS, SUB_NUMBER } from './contants(未拆分)'

export const addNumberAction = (number) => ({
  type: ADD_NUMBER,
  number
})

export const subNumberAction = (number) => ({
  type: SUB_NUMBER,
  number
})

export const changeBannersAction = (banners) => ({
  type: CHANGE_BANNERS,
  banners: banners
})

export const changeRecommendsAction = (recommends) => ({
  type: CHANGE_RECOMMENDS,
  recommends: recommends
})

//使用了 redux-thunk 之后，可以派发函数
export const fetchHomeMultidataAction = () => {

  //如果是一个普通的action，那么需要返回一个action对象， return {}
  //存在问题：返回对象中，是不能直接拿到从服务器请求的异步数据的

  //如果返回的是一个函数，redux是不支持的，不能进行dispatch
  //所以如果想要将一个函数作为action对象进行派发，那么需要一个中间件: redux-thunk
  return (dispatch, getState) => {
    //发送异步网络请求
    axios.get("http://123.207.32.32:8000/home/multidata").then(res => {
      const banners = res.data.data.banner.list;
      const recommends = res.data.data.recommend.list;
      //将请求到的数据派发到store
      dispatch(changeBannersAction(banners));
      dispatch(changeRecommendsAction(recommends));
    })
    
  }
}